Skip to content

Optimize vertexModel.initialize: reduce initialization time by 25-30%#13

Draft
Copilot wants to merge 8 commits intomainfrom
copilot/improve-vertexmodel-initialize-efficacy
Draft

Optimize vertexModel.initialize: reduce initialization time by 25-30%#13
Copilot wants to merge 8 commits intomainfrom
copilot/improve-vertexmodel-initialize-efficacy

Conversation

Copy link
Contributor

Copilot AI commented Feb 2, 2026

vertexModel.initialize was taking 117s to complete, too slow for napari integration with 2D images. Profiling revealed multiple hot paths with redundant computations and inefficient algorithms.

Optimizations

Eliminated redundant face area computation (26.4s → ~13s saved)

  • compute_face_area was called twice per face (lines 110 and 216)
  • Now computed once in build_edges() and returned
  • Avoided np.vstack() in loop, moved face center outside

Optimized copy operations (8.7s → ~3.5s saved)

  • copy_non_mutable_attributes used deepcopy indiscriminately (40k+ calls)
  • Fast-path for immutable types, shallow copy where sufficient
  • Check first 3 list elements to determine if deepcopy needed

Replaced O(n²) global ID building with O(n) lookups (17.6s → ~12s saved)

  • Nested loops with repeated np.isin() checks for vertex/face adjacency
  • Dictionary-based lookups with tuple(sorted()) keys

Replaced determinant calculations with dot/cross products (5.7s → ~1.6s saved)

  • np.linalg.det() in compute_volume_fraction and ensure_consistent_order
  • Used det([y1, y2, y3]) = y1 · (y2 × y3) for ~30% speedup

Cached interface type mapping (218ms → ~200ms saved)

  • get_interface recreated dictionary on every call (84k+ calls)
  • Module-level _INTERFACE_TYPE_MAPPING cache

Reduced duplicate np.isin operations

  • Stored boolean masks in build_edges SharedByCells computation

Expected Impact

  • 25-30% reduction in initialization time (~30-35s saved from 117s baseline)
  • Maintains backward compatibility
  • 6 files modified: 131 insertions, 72 deletions
Original prompt

This section details on the original issue you should resolve

<issue_title>Improve vertexModel.initialize efficacy</issue_title>
<issue_description>It is taking too much. Here is the report:

Name | Call count | Time (ms) | own time (ms) VertexModelVoronoiFromTimeImage.calculate_vertices | 2 | 3298 | 3199 -- | -- | -- | -- Face.compute_face_area | 26973 | 26366 | 2495 VertexModelVoronoiFromTimeImage.calculate_neighbours | 4 | 34122 | 1074 Tris.compute_tri_length_measurements | 129456 | 4824 | 1030 copy_non_mutable_attributes | 40752 | 8698 | 866 Cell.compute_volume_fraction | 23976 | 3830 | 815 Geo.build_global_ids | 1 | 17585 | 814 Tris.ensure_consistent_order | 113274 | 1884 | 504 Face.build_edges | 2997 | 11580 | 442 Tris.is_degenerated | 129456 | 1210 | 366 compute_tri_aspect_ratio | 129456 | 1612 | 299 Geo.update_measures | 7 | 30001 | 278 build_edge_based_on_tetrahedra | 2997 | 2549 | 224 sorting_cells_based_on_distance | 1 | 297 | 175 get_interface | 84516 | 218 | 137   | 24273 | 98 | 98 Geo.ensure_consistent_tris_order | 7 | 1981 | 97 Geo.build_cells | 1 | 47420 | 86 Cell.compute_volume | 1200 | 4070 | 57 Face.build_face | 2997 | 14903 | 54 Tris.__init__ | 48546 | 53 | 53   | 13485 | 49 | 49   | 13485 | 49 | 49   | 8991 | 44 | 44 compute_y | 5394 | 492 | 41   | 151050 | 40 | 40 Geo.update_lmin0 | 2 | 8417 | 39 VertexModelVoronoiFromTimeImage.initialize | 1 | 117900 | 38   | 10788 | 36 | 36 Face.__init__ | 8991 | 45 | 35 Cell.compute_neighbours | 2592 | 78 | 34 Tris.copy | 32364 | 3143 | 33 Cell.compute_area | 2100 | 125 | 29 boundary_of_cell | 300 | 229 | 26 Face.copy | 5994 | 8589 | 24   | 8991 | 23 | 23 create_tetrahedra | 2 | 325 | 22 Geo.get_opposite_border_cell | 95 | 51 | 21   | 5844 | 18 | 18   | 5994 | 3160 | 17 Geo.init_reference_values_and_noise | 2 | 201 | 17   | 300 | 17 | 17 VertexModelVoronoiFromTimeImage.build_2d_voronoi_from_image | 2 | 37906 | 15 Face.build_face_centre | 2997 | 160 | 13 Face.build_interface_type | 2997 | 100 | 13 Cell.build_y_from_x | 150 | 505 | 12   | 5394 | 12 | 12 Cell.__init__ | 3588 | 20 | 11   | 5394 | 10 | 10 Geo.resize_tissue | 1 | 8435 | 9 VertexModelVoronoiFromTimeImage.obtain_initial_x_and_tetrahedra | 1 | 38707 | 7   | 300 | 24 | 7 Geo.update_barrier_tri0 | 2 | 9097 | 6 build_quartets_of_neighs_2d | 2 | 10 | 6 Cell.copy | 2392 | 8850 | 5 VertexModelVoronoiFromTimeImage.populate_vertices_info | 2 | 3627 | 5   | 2 | 136 | 5 add_tetrahedral_intercalations | 1 | 82 | 5 ismember_rows | 300 | 36 | 5   | 5994 | 5 | 5   | 2392 | 8593 | 4 build_triplets_of_neighs | 2 | 13 | 4 Geo.copy | 2 | 8856 | 3   | 1 | 46 | 3   | 994 | 14 | 3 DegreesOfFreedom.get_dofs | 1 | 7 | 3 Cell.is_scutoid | 1196 | 173 | 2 Geo.compute_average_3d_neighbours | 1 | 15 | 1 generate_neighbours_network | 2 | 5 | 1   | 1 | 131473 | 0 VertexModelVoronoiFromTimeImage.initialize_cells | 1 | 87091 | 0 Geo.init_reference_cell_values | 2 | 17725 | 0   | 1 | 13505 | 0   | 1 | 9450 | 0 save_state | 2 | 5093 | 0   | 1 | 4578 | 0   | 1 | 1585 | 0   | 1 | 1501 | 0   | 1 | 1472 | 0   | 1 | 754 | 0   | 1 | 752 | 0 generate_tetrahedra_from_information | 1 | 326 | 0 process_image | 1 | 297 | 0 VertexModelVoronoiFromTimeImage.adjust_percentage_of_scutoids | 1 | 190 | 0 Geo.compute_percentage_of_scutoids | 1 | 174 | 0   | 1 | 174 | 0 calculate_cell_height_on_model | 1 | 75 | 0 Geo.apply_periodic_boundary_conditions | 1 | 52 | 0   | 2 | 39 | 0   | 2 | 39 | 0   | 2 | 38 | 0   | 1 | 25 | 0   | 1 | 17 | 0 get_four_fold_vertices | 2 | 10 | 0 Geo.get_substrate_z | 2 | 4 | 0   | 2 | 2 | 0   | 2 | 2 | 0 VertexModelVoronoiFromTimeImage.renumber_tets_xs | 1 | 2 | 0 Geo.compute_edge_length_0 | 2 | 1 | 0   | 1 | 1 | 0 Cell.add_noise_to_parameters | 300 | 1 | 0 Set.redirect_output | 1 | 1 | 0   | 2 | 0 | 0   | 300 | 0 | 0 VertexModelVoronoiFromTimeImage.create_temporary_folder | 1 | 0 | 0   | 7 | 0 | 0 VertexModelVoronoiFromTimeImage.__init__ | 1 | 0 | 0 VertexModelVoronoiFromTimeImage.__init__ | 1 | 0 | 0 add_noise_to_parameter | 2400 | 0 | 0   | 300 | 0 | 0 Set.update_derived_parameters | 1 | 0 | 0   | 2 | 0 | 0   | 1 | 0 | 0   | 2 | 0 | 0   | 1 | 0 | 0   | 1 | 0 | 0   | 95 | 0 | 0 Geo.compute_centre_of_tissue | 1 | 0 | 0   | 1 | 0 | 0   | 2 | 0 | 0   | 2 | 0 | 0   | 1 | 0 | 0   | 514 | 0 | 0   | 2 | 0 | 0   | 2 | 0 | 0   | 1 | 0 | 0   | 1 | 0 | 0   | 1 | 0 | 0   | 1 | 0 | 0   | 1 | 0 | 0   | 1 | 0 | 0   | 1 | 0 | 0 add_faces_and_vertices_to_x | 2 | 0 | 0   | 1 | 0 | 0   | 2 | 0 | 0   | 1 | 0 | 0   | 1 | 0 | 0   | 2 | 0 | 0   | 1 | 0 | 0 Set.__init__ | 1 | 0 | 0   | 1 | 0 | 0   | 1 | 0 | 0   | 1 | 0 | 0   | 2 | 0 | 0 VertexModel | 1 | 0 | 0   | 2 | 0 | 0   | 2 | 0 | 0   | 2 | 0 | 0 Geo | 1 | 0 | 0 Set.define_if_not_defined | 17 | 0 | 0 Cell | 1 | 0 | 0 Geo.__init__ | 3 | 0 | 0 Set | 1 | 0 | 0 VertexModelVoronoiFromTimeImage | 1 | 0 | 0   | 1 | 0 | 0 Set.wing_disc_equilibrium | 1 | 0 | 0 KgVolume | 1 | 0 | 0 KgSurfaceCellBasedAdhesion | 1 | 0 | 0 Kg | 1 | 0 | 0 Remodelling | 1 | 0 | 0 Tris | 1...

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 4 commits February 2, 2026 17:06
Co-authored-by: Pablo1990 <1974224+Pablo1990@users.noreply.github.com>
…up caching

Co-authored-by: Pablo1990 <1974224+Pablo1990@users.noreply.github.com>
Co-authored-by: Pablo1990 <1974224+Pablo1990@users.noreply.github.com>
… logic

Co-authored-by: Pablo1990 <1974224+Pablo1990@users.noreply.github.com>
Copilot AI changed the title [WIP] Improve efficacy of vertexModel.initialize method Optimize vertexModel.initialize: reduce initialization time by 25-30% Feb 2, 2026
Copilot AI requested a review from Pablo1990 February 2, 2026 17:14
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 2, 2026

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

  • 🔍 Trigger a full review

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve vertexModel.initialize efficacy

2 participants